define(function(require, exports, module) {
	require("bootstrap");
	require("lib/common/yisin");
	top.data.dialog = require("lib/artDialog/dialog-plus.js");
	var action = require("./action");
	var browserCheck = false;
	 if (!window.applicationCache) {
		 top.data.loginDialog = top.data.dialog({
			id: 'check-dialog',
			title: '浏览器检查',
			content: '<h2>您的浏览器版本过低，请更换浏览器，本程序是使用JsSocket组件开发，JsSocket本身是兼容低版本浏览器，但本程序页面使用了一些CSS样式不兼容低版本IE浏览器<br>建议使用Chrome、Firefox等内核浏览器</h2><br>'
				+ '下载地址：<a href="http://www.pc6.com/softview/softview_24670.html" target="_blank">Chrome</a>'
				+ '&nbsp;&nbsp;<a href="http://www.firefox.com.cn/download/" target="_blank">Firefox</a><br><br>',
			width: 550, height: 230, 
			okDisplay: false, escClose: false,
			cancelDisplay: false, cancel: false
		}).showModal();
		return;
     }
	 browserCheck = true;
	
	var pid = CommonUtil.getRequestParam("u");
	var pageid = CommonUtil.getCookie("cuserId");
	var msgnum = 500;
	
	if(typeof(pid) == 'object' || pid.length != 10 || pid != pageid){
		pageid = pageid || CommonUtil.UUID(10, 16);
		CommonUtil.addCookie("cuserId", pageid, 30);
		var url = document.location.href;
		url = url.substring(0, url.indexOf('?'));
		document.location.href = url + '?u=' + pageid;
		return;
	}
	top.pageid = pid;
	
	var showLogin = function(){
		top.data.loginDialog = top.data.dialog({
			id: 'login-dialog',
			title: '描述下自己',
			page: {url: top.data.ctx + '/pages/chats/info.html'},
			width: 500, height: 440, 
			okDisplay: false, escClose: false,
			cancelDisplay: false, cancel: false
		}).showModal();
	};
	
	/**
	 * 初始化JsSocket组件
	 */
	top.initChat = function(){
		if(!yinsin.inited){
			var inval = window.setInterval(function(){
				console.info("JsSocket组件加载完成", yinsin.status);
				if(yinsin.status == 1){
					yinsin.initJsSocket(function(){
						yinsin.sendData('cmd●loginchat□' + JSON.stringify(top.user));
					}, function(){
						alert('与服务器断开连接');
					}, serverMsg);
					
					yinsin.initFlashConfig({
						serverIp: '127.0.0.1', //127.0.0.1,
						serverPort: 8283,
						serverPath: ''
					});
					yinsin.inited = true;
					window.clearInterval(inval);
				}
			}, 1000);
		}
	};
	
	top.saveUserInfo = function(){
		try{
			CommonUtil.addCookie("cuserId", top.user.userId, 30);
			CommonUtil.addCookie("cname", top.user.name, 30);
			CommonUtil.addCookie("cmobile", top.user.mobile, 30);
			CommonUtil.addCookie("cemail", top.user.email, 30);
			CommonUtil.addCookie("cqq", top.user.qq, 30);
			CommonUtil.addCookie("cheader", top.user.header, 30);
			
			console.info("userId=", CommonUtil.getCookie("cuserId"), top.user);
		} catch (e){
			console.info("set user info to Cookie Error", e);
		}
	};
	
	if(browserCheck){
		if(top.user){
			top.initChat();
		} else {
			var userId = CommonUtil.getCookie("cuserId");
			var name = CommonUtil.getCookie("cname");
			var mobile = CommonUtil.getCookie("cmobile");
			var email = CommonUtil.getCookie("cemail");
			var header = CommonUtil.getCookie("cheader");
			var sex = CommonUtil.getCookie("csex");
			var qq = CommonUtil.getCookie("cqq");
			top.user = {
	    		userId: userId,
	    		name: name,
	    		mobile: mobile,
	    		email: email,
	    		qq: qq,
	    		sex: sex,
	    		header: header
	    	};
			console.info("user", top.user);
			if(userId && userId.length == 10 && name){
				top.initChat();
			} else {
				showLogin();
			}
		}
	}
	
	
	var businName = {
		's0': '登录成功', 
		's1': '用户登录', 
		's2': '用户退出', 
		's3': '聊天消息', 
		's4': '动作消息', 
		's5': '异常退出', 
		's6': '用户列表',
		's10': '系统消息'
	};
	
	var serverMsg = function(data){
		try{
			data = decodeURI(decodeURI(data));
			data = unescape(data);
			try{
				//console.info("data=========", data);
				data = JSON.parse(data);
				console.info(businName['s' + data.genre], data);
			}catch(e){
				console.debug(e);
				data = {};
			}
			if(data.genre == 0){// 自己登录成功
				loginSuccess(data);
			} else if(data.genre == 1){ // 其他用户登录
				pushUserToList(data.user);
				showUserList();
				showPersonNum();
			} else if(data.genre == 2){ // 其他用户退出
				$('#' + data.userId).attr('exit', '0').addClass('user-out');
				delUserForList(data);
				showPersonNum();
			} else if(data.genre == 3){ // 消息
				if(data.fromId != top.user.userId){
					showMessage({
						'flag' : data.flag,
						'fromName' : data.fromName,
						'userId' : data.fromId,
						'msg' : data.msg,
						'time' : data.time
					});
					
				}
			} else if(data.genre == 4){ // 动作
				if(data.touser == top.user.userId){
					exeActions(data);
				}
			} else if(data.genre == 5){ // 异常退出
				$('#' + data.userId).attr('exit', '0').addClass('user-out');
				delUserForList(data);
				showPersonNum();
			} else if(data.genre == 6){ // 用户列表
				if(!top.userList){
					top.userList = [];
				}
				var uid = '';
				for(var i = 0, k = data.userList.length; i < k; i++){
					uid = data.userList[i].userId;
					if(getUserByList(uid) == null){
						pushUserToList(data.userList[i]);
					}
				}
				showUserList();
			} else if(data.genre == 10){ // 系统消息
				showSystemMsg(data);
			}
		}catch(e){
			console.debug(e);
		}
	};
	
	function showPersonNum(){
		top.userList && $('#alluserid .user-all').text('所有人（'+top.userList.length+'）');
	}
	
	var pushUserToList = function(user){
		if(!top.userList){
			top.userList = [];
		}
		var u = {}, has = false;
		for(var i = 0, k = top.userList.length; i < k; i++){
			u = top.userList[i];
			if(u.userId == user.userId){
				has = true;
				u.name = user.name;
				u.mobile = user.mobile;
				u.email = user.email;
				u.header = user.header;
				u.sex = user.sex;
				u.city = user.city;
				u.ip = user.ip;
				break;
			}
		}
		if(!has){
			top.userList.push(user);
		}
	};
	
	var delUserForList = function(user){
		var u = {}, has = false;
		for(var i = 0, k = top.userList.length; i < k; i++){
			u = top.userList[i];
			if(u.userId == user.userId){
				top.userList.splice(i, 1);
				break;
			}
		}
	};
	
	var getUserByList = function(userId){
		if(top.userList){
			var u = {}, has = false;
			for(var i = 0, k = top.userList.length; i < k; i++){
				u = top.userList[i];
				if(u.userId == userId){
					return u;
				}
			}
		}
		return null;
	};
	
	var getUserList = function(){
		Common.ajax({
			url: top.data.ctx + '/chats!getUserList',
			param: {"userId": top.user.userId},
			dataType: 'json',
			success: function(d, jd){
				top.userList = jd.data;
				
				showUserList();
			}
		});
	};
	
	var loginSuccess = function(data){
		var u = data.user;
		top.user = u;
		//yinsin.sendData('cmd●getUserList□' + u.userId);
		
		getUserList();
		setInterval(getUserList, 1000* 120);
	};
	
	document.onkeydown = function(e) {
		e = window.event || e;
		var keycode = e.keyCode || e.which;
		if (e.ctrlKey || e.altKey || e.shiftKey || (keycode >= 112 && keycode <= 123)) {
			if (window.event) {// ie
				try {
					e.keyCode = 0;
				} catch (e) {
				}
				e.returnValue = false;
			} else {// ff
				e.preventDefault();
			}
		}
	};
	
	window.onbeforeunload = function(){
		return "您确定要退出吗？";
	};
	window.onunload = function(){
		 
	};
//	document.oncontextmenu = function(){return false;};

	$('#content').on('keyup', function(e) {
		if (e.ctrlKey && e.keyCode == 13) {
			send();
		}
	});
	
	$('#sendBut').on('click', send);

	var fontFamily = 'Arial';
	var fontColor = '#000000';
	var fontSize = '2';
	var touser = "all_user", 
	touserName = '大家';
	function send() {
		var userId = top.user.userId;
		var content = $('#content').html();
		touser = $('#touser-id').text() || "all_user";
		if(!content){
			return;
		}
		var chats = {
			'face': fontFamily,
			'size': fontSize,
			'color': fontColor, 
			'content': content
		};
		var info = {
			"touser" : touser,
			'fromuser' : userId,
			"msg" : chats
		};
		touserName = $('#touser-name').text() || '大家';
		if($('#'+touser).attr('exit')){
			showErrorInfo('&nbsp;&nbsp;<span class="tou">' + touserName +'('+ touser + ')</span> 已断开连接');
			$('#sendBut').attr('disabled', true);
			setTimeout(function(){
				$('#sendBut').attr('disabled', false);
			}, 2000);
			return;
		}
		var chatType = touser == "all_user" ? 'chatall' : 'chat';
		touserName = touser == "all_user" ? '大家' : touserName;
		var msg = 'cmd●chat□' + JSON.stringify(info);
		$('#content').html('');
		if (!!chats) {
			yinsin.sendData(msg);
			var d = new Date();
			var time = d.getFullYear() + '-' + (d.getMonth() + 1) + '-'
					+ d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes()
					+ ':' + d.getSeconds();
			showMessage({
				'flag' : 3,
				'fromName' : touserName,
				'userId' : touser,
				'msg' : chats,
				'time' : time
			});
			$('#sendBut').attr('disabled', true).addClass('disabled');
			setTimeout(function(){
				$('#sendBut').attr('disabled', false).removeClass('disabled');
			}, 1000);
		} else {
			alert("发送的内容不能为空！");
		}
	}
	
	top.isChatIng = true;
	startThread();
	function startThread() {
		var $ulist = $('.userDv');
		if($ulist.length){
			var $this, exit = 0, uid;
			$ulist.each(function(){
				$this = $(this);//user-out
				if(!$this.hasClass('all_user') && !$this.hasClass('my')){
					exit = $this.attr('exit');
					uid = $this.attr('id');
					var us = getUserByList(uid);
					if(exit || !us){
						exit = parseInt(exit);
						if(exit >= 1 || !us){
							if(uid == $('#touser-id').text()){
								top.chooseUser($('#alluserid'));
							}
							if(exit < 2){
								$this = $('#' + uid);
							}
							var $parent = $this.parent();
							if($parent && $parent.find('userDv').length == 0){
								$parent.prev().remove();
								$parent.remove();
							} else {
								$this.remove();
							}
							delUserForList(user);
						} else {
							exit++;
							$this.attr('exit', exit);
						}
					}
				}
			});
		}
		setTimeout(startThread, 5000);
		SetPanelStyle && SetPanelStyle();
	}

	top.scrollInfo = function() {
		$('.message[index=' + ((msgindex - msgnum)) +']').remove();
		var div = document.getElementById('info');
		if (div.scrollHeight > div.offsetHeight) {
			div.scrollTop = div.scrollHeight - div.offsetHeight;
		}
	};

	function showMessage22222(data) {
		var flag = data.flag;
		var name = data.fromName;
		var userId = data.userId;
		var fromId = data.fromId;
		var message = data.msg;
		var $info = $('#info');
		var text = document.getElementById("info").innerHTML;
		var $pan = null;
		alert(userId);
		switch(flag){
			case 1:
				var u = getUserByList(fromId);
				$pan = $('<div class="talk_recordbox">'
					+ '	<div class="user" uid="'+u.userId+'"><img src="'+top.data.ctx+'/'+u.header+'"/>'+name+'</div>'
					+ '	<div class="talk_recordtextbg">&nbsp;</div>'
					+ '	<div class="talk_recordtext">'
					+ '		<h3>'+formatMsg(message)+'</h3>'
					+ '		<span class="talk_time">'+data.time+'</span>'
					+ '	</div>'
					+ '</div>');
				$info.append($pan);
			break;
			case 2:
				var u = getUserByList(fromId);
				$pan = $('<div class="talk_recordbox">'
					+ '	<div class="user" uid="'+u.userId+'"><img src="'+top.data.ctx+'/'+u.header+'"/>'+name+'</div>'
					+ '	<div class="talk_recordtextbg">&nbsp;</div>'
					+ '	<div class="talk_recordtext">'
					+ '		<h3>'+formatMsg(message)+'</h3>'
					+ '		<span class="talk_time">'+data.time+'</span>'
					+ '	</div>'
					+ '</div>');
				$info.append($pan);
			break;
			default:
				var u = top.user;
				$pan = $('<div class="talk_recordboxme">'
					+ '	<div class="user" uid="'+u.userId+'"><img src="'+top.data.ctx+'/'+u.header+'"/>'+u.name+'</div>'
					+ '	<div class="talk_recordtextbg">&nbsp;</div>'
					+ '	<div class="talk_recordtext">'
					+ '		<h3>'+formatMsg(message)+'</h3>'
					+ '		<span class="talk_time">'+data.time+'</span>'
					+ '	</div>'
					+ '</div>');
				$info.append($pan);
			break;
		}
		scrollInfo();
		$pan && $pan.find('.user').on('click', function(e){
			var uid = $(this).attr('uid');
			var $touser = $('#' + uid);
			var $parent = $touser.parent();
			if($parent.is(":hidden")){
				$parent.show();
				$parent.prev().removeClass('close');
			}
			top.chooseUser($touser);
			$('#userListDiv').animate({scrollTop: $touser.offset().top}, 1000);
		});
	}
	
	var msgindex = 0;
	function showMessage(data) {
		var fuser = getUserByList(data.userId);
		var flag = data.flag;
		var msgType = $('#msg-type').val();
		var ucity = '';
		if(fuser != null && flag != 3){
			ucity = fuser.city;
			if(msgType == 'city'){
				if(ucity != top.user.city){
					return;
				}
			} else if(msgType == 'chat'){
				if(flag != 2){
					return;
				}
			}
		}
		var name = data.fromName;
		var userId = data.userId;
		var message = data.msg;
		var $info = $('#info');
		var text = document.getElementById("info").innerHTML;
		var $us1 = null, $msg;
		switch(flag){
			case 1:
				var msgdiv = '<div class="message" index="' + (msgindex++) + '" city="'+ucity+'"></div>';
				$us1 = $('<span class="tou atuser" uid="'+userId+'">[' + name + ']</span>');
				var to = '&nbsp;' + data.time + '&nbsp;对<span class="tou">[大家]</span>';
				$msg = $(msgdiv).append($us1).append('('+ucity+')').append(to + '说：' + formatMsg(message));
				$info.append($msg);
			break;
			case 2:
				var msgdiv = '<div class="message tome" index="' + (msgindex++) + '" city="'+ucity+'"></div>';
				$us1 = $('<span class="tou atuser" uid="'+userId+'">[' + name + ']</span>');
				var to = '&nbsp;' + data.time + '&nbsp;对<span class="sme">[您]</span>';
				$msg = $(msgdiv).append($us1).append('('+ucity+')').append(to + '说：' + formatMsg(message));
				$info.append($msg);
			break;
			default:
				var msgdiv = '<div class="message meto" index="' + (msgindex++) + '" city="'+ucity+'"></div>';
				var us2 = '<span class="sme">[您]</span>';
				var to = '&nbsp;' + data.time + '&nbsp;对';
				if(userId == 'all_user'){
					var us3 = '<span class="tou" uid="'+userId+'">[' + name + ']</span>';
					$msg = $(msgdiv).append(us2 + to).append(us3).append('说：' + formatMsg(message));
					$info.append($msg);
				} else {
					$us1 = $('<span class="tou atuser" uid="'+userId+'">[' + name + ']</span>');
					$msg = $(msgdiv).append(us2 + to).append($us1).append('说：' + formatMsg(message));
					$info.append($msg);
				}
			break;
		}
		scrollInfo();
		
		/*$us1 && $us1.on('click', function(e){
			var uid = $(this).attr('uid');
			var $touser = $('#' + uid);
			if($touser.length > 0){
				var $parent = $touser.parent();
				if($parent.is(":hidden")){
					$parent.show();
					$parent.prev().removeClass('close');
				}
				top.chooseUser($touser);
				$('#userListDiv').animate({scrollTop: $touser.offset().top}, 1000);
			}
		}).on('mouseover', function(e){
			top.showUserInfoPanal(e, $(this));
		}).on('mouseout', function(e){
			top.hiddenUserInfoPanal($(this));
		});*/
		
		$msg && $msg.find('.tou.atuser').on('click', function(e){
			var uid = $(this).attr('uid');
			var $touser = $('#' + uid);
			if($touser.length > 0){
				var $parent = $touser.parent();
				if($parent.is(":hidden")){
					$parent.show();
					$parent.prev().removeClass('close');
				}
				top.chooseUser($touser);
				$('#userListDiv').animate({scrollTop: $touser.offset().top}, 1000);
			}
		}).on('mouseover', function(e){
			top.showUserInfoPanal(e, $(this));
		}).on('mouseout', function(e){
			top.hiddenUserInfoPanal($(this));
		});
	}
	
	function showSystemMsg(data) {
		var d = data.time;
		var text = d + "&nbsp;" + data.msg;
		var $msg = $('<div class="message sys-msg" index="' + (msgindex++) + '">' + text + '</div>');
		$("#info").append($msg);
		scrollInfo();
		
		$msg && $msg.find('.tou.atuser').on('click', function(e){
			var uid = $(this).attr('uid');
			var $touser = $('#' + uid);
			if($touser.length > 0){
				var $parent = $touser.parent();
				if($parent.is(":hidden")){
					$parent.show();
					$parent.prev().removeClass('close');
				}
				top.chooseUser($touser);
				$('#userListDiv').animate({scrollTop: $touser.offset().top}, 1000);
			}
		}).on('mouseover', function(e){
			top.showUserInfoPanal(e, $(this));
		}).on('mouseout', function(e){
			top.hiddenUserInfoPanal($(this));
		});
	}
	
	function showErrorInfo(text) {
		var d = new Date();
		$("#info").append('<div class="message errorInfo" index="' + (msgindex++) + '">' + text + '</div>');
		scrollInfo();
	}

	function formatMsg(msg) {
		// face=Arial;size=2;color=#000000;content=无穷大地区稳定的我大旗网的请问
		var html = '<div class="msg-con">' + msg + '</div>';
		if(typeof(msg) == 'object'){
			var font = '<font face="'+msg.face+'" size="'+msg.size+'" color="'+msg.color+'">' + msg.content + '</font>';
			html = '<div class="msg-con">' + font + '</div>';
		}
		return html;
	}
	
	$('#clearRecordBtn').on('click', function(){
		$('div.message').remove();
		msgindex = 0;
	});
	
	$('#msg-type').on('change', function(){
		var text = $(this).find('option:selected').text();
		showErrorInfo('显示：' + text);
	});
	
	$('#fontDv,#faceDv,#actionDv,#imgDv,#medioDv').hover(function(){
		$(this).css({
			'border-bottom': '2px double #aaaaaa',
			'border-right': '2px double #aaaaaa'
		});
	}, function(){
		$(this).css({
			'border-bottom': '0',
			'border-right': '2px double #ffffff'
		});
	});
	
	function addFace($t) {
		var imageurl = $t.attr('value');
		$('#content').append('<img src="' + imageurl + '" />');
		showFacePanal();
	}

	// 格式化编辑器中的内容
	$('#editTool button').on('click', function() {
		var opt, what = $(this).val();
		if (opt == "RemoveFormat") {
			what = opt;
			opt = null;
		}
		if (opt == null) {
			eWebEditor.document.execCommand(what);
		} else {
			eWebEditor.document.execCommand(what, false, opt);
		}
	});
	
	var findDom = function(id, att, value){
		var count = 0;
		var $dom = $(id);
		$dom && $dom.each(function(){
			var $this = $(this);
			if($this.attr(att) == value){
				count++;
			}
		});
		return count;
	};

	function showUserList() {
		var u = {};
		try{
			for(var i = 0, k = top.userList.length; i < k; i++){
				u = top.userList[i];
				if(u.userId != top.user.userId){
					if($('#' + u.userId).length == 0){
						var $panel = {};//$('.userListPanel[city=' + u.city+']');
						var cnum = findDom('div.userListPanel', 'city', u.city);
						if(cnum == 0){
							var $city = $('<div class="city-line">≡≡≡【'+u.city+'】≡≡≡</div>');
							$panel = $('<div class="userListPanel" city="'+u.city+'">');
							$("#userListDiv").append($city).append($panel);
							$city.on('click', function(){
								$this = $(this);
								if($this.hasClass('close')){
									$this.next().show();
									$this.removeClass('close');
								} else {
									$this.next().hide();
									$this.addClass('close');
								}
							});
						} else {
							$panel = $('.userListPanel[city=' + u.city+']');
						}
						var htmls = '<div id="'+u.userId+'" class="userDv">';
						htmls += '<img src="' + top.data.ctx + '/' + u.header 
							+ '" class="ulist-header-img" width="18" height="18">';
						htmls += '<span class="user-span">'+ u.name + '(' + u.userId + ')</span></div>';
						var $htmls = $(htmls);
						$panel.append($htmls);
						$htmls.on('mouseover', function(e){
							top.showUserInfoPanal(e, $(this));
						}).on('mouseout', function(e){
							top.hiddenUserInfoPanal($(this));
						}).on('click', function(){
							top.chooseUser($(this));
						});
					} else {
						$('#' + u.userId).attr('exit', '').removeClass('user-out');
					}
				} else {
					$('.userDv.my').attr('id', u.userId);
					$('.user-my').html(u.name + '('+u.userId+')--自己');
					$('.userDv.my img').attr('src', top.data.ctx + '/' + u.header);
				}
			}
		}catch(e){
			console.debug(e);
		}
		$('#alluserid .user-all').text('所有人（'+top.userList.length+'）');
		
		$('#load-user-wait').remove();
	}
	
	$('.userDv.my').on('mouseover', function(e){
		top.showUserInfoPanal(e, $(this));
	}).on('mouseout', function(e){
		top.hiddenUserInfoPanal($(this));
	});

	top.showUserInfoPanal = function(e, $this) {
		var uid = $this.attr('id') || $this.attr('uid');
		var u = getUserByList(uid);
		if(u == null){
			return;
		}
		$this.css('border', '1px double #b98841');
		$('#u_headImg').attr('src', top.data.ctx + '/' + u.header);
		$('#u_name').html('姓名：' + u.name);
		$('#u_area').html('来自：' + u.city);
		$('#u_phone').html('电话：' + u.mobile);
		$('#u_email').html('邮箱：' + u.email);
		$('#u_qq').html('QQ：' + u.qq);
		$('#u_ip').html('性别：' + u.sex);
		e = e || window.event;
		var islist = $this.parent().hasClass('userListPanel');
		$('#userInfoPanal').css({
			'left': islist ? '258px' : (e.clientX + 2)+ 'px', 
			'top': (e.clientY + (islist ? 0 : 5)) + 'px'
		}).show();
	};
	
	top.hiddenUserInfoPanal = function($this) {
		if($this.parent().hasClass('userListPanel')){
			$this.css('border', '1px double #ffffff');
		} else {
			$this.css('border', '0');
		}
		$('#userInfoPanal').hide();
	};
	
	$('#alluserid').on('click', function(){
		top.chooseUser($(this));
	});
	
	top.chooseUser = function($this){
		$('.userDv').removeClass('choose');
		$this.addClass('choose');
		var uid = $this.attr('id');
		if(uid == 'alluserid'){
			$('#touser-name').text('所有人');
			$('#touser-id').text('all_user');
		} else {
			var u = getUserByList(uid);
			if(u == null){
				return;
			}
			$('#touser-name').text(u.name);
			$('#touser-id').text(u.userId);
		}
	};

	$('#toolDiv > div > img').hover(function() {
		$(this).parent().css({
			'border-bottom' : '2px double #aaaaaa',
			'border-right' : '2px double #aaaaaa'
		});
	}, function() {
		$(this).parent().css({
			'border-bottom' : '0px',
			'border-right' : '2px double #ffffff'
		});
	});
	
	$('#fontDv').on('click', function(){
		showFontPanal();
	});

	var FontPanal = null;
	function showFontPanal() {
		if (FontPanal == null) {
			var html = '&nbsp;设置：字体：<select id="choseFont">';
			html += '<option value="System">System</option>';
			html += '<option value="宋体">宋体</option>';
			html += '<option value="楷体">楷体_GB2312</option>';
			html += '<option value="隶书">隶书</option>';
			html += '<option value="幼圆">幼圆</option>';
			html += '<option value="黑体">黑体</option>';
			html += '<option value="微软雅黑">微软雅黑</option>';
			html += '<option value="华文彩云">华文彩云</option>';
			html += '<option value="华文新魏">华文新魏</option>';
			html += '<option value="方正舒体">方正舒体</option><select>';
			html += '，颜色：<select id="choseColor">';
			html += '<option value="#000000">黑色</option>';
			html += '<option value="#ff0000">红色</option>';
			html += '<option value="#0000ff">蓝色</option>';
			html += '<option value="#00ff00">绿色</option>';
			html += '<option value="#ffff00">黄色</option>';
			html += '<option value="#ff00ff">紫色</option>';
			html += '<option value="#ff6c00">橙色</option>';
			html += '<option value="#aaaaaa">灰色</option>';
			html += '<option value="#01fff0">青色</option><select>';
			html += '，大小：<select id="choseSize">';
			html += '<option value="8">小二</option>';
			html += '<option value="7">二号</option>';
			html += '<option value="6">小三</option>';
			html += '<option value="5">三号</option>';
			html += '<option value="4">小四</option>';
			html += '<option value="3">四号</option>';
			html += '<option value="2" selected>小五</option><select>';
			html += '&nbsp;<span><a id="setFontColse" href="javascript:void(0);">关闭</a></span>';
			FontPanal = document.createElement("div");
			FontPanal.style.position = 'absolute';
			FontPanal.style.width = '430px';
			FontPanal.style.border = '1px double #b98841';
			FontPanal.style.lineHeight = '25px';
			FontPanal.style.backgroundColor = '#eeeeee';
			FontPanal.style.left = '275px';
			FontPanal.style.bottom = '100px';
			FontPanal.style.fontSize = '12px';
			FontPanal.innerHTML = html;
			document.body.appendChild(FontPanal);
			
			$('#choseFont').on('change', function(){
				changeFontFamily($(this));
			});
			$('#choseColor').on('change', function(){
				changeFontColor($(this));
			});
			$('#choseSize').on('change', function(){
				changeFontSize($(this));
			});
			$('#setFontColse').on('click', function(){
				showFontPanal();
			});
		} else {
			FontPanal.style.display = FontPanal.style.display == 'none' ? 'block' : 'none';
		}
	}

	function changeFontFamily($this) {
		fontFamily = $this.val();
	}

	function changeFontColor($this) {
		fontColor = $this.val();
	}

	function changeFontSize($this) {
		fontSize = $this.val();
	}

	var fontSizeNum = 2;
	function fontChange(n) {
		if (n == 1) {
			if (fontSizeNum < 50) {
				fontSizeNum++;
				format("fontsize", fontSizeNum);
			}
		} else if (n == 2) {
			if (fontSizeNum > 0) {
				fontSizeNum--;
				format("fontsize", fontSizeNum);
			}
		} else if (n == 3) {
			ShowDialog('selcolor.htm?.3=forecolor', 280, 250, true);
		} else if (n == 4) {
			ShowDialog('selcolor.htm?action=backcolor', 280, 250, true);
		}
	}

	// 显示无模式对话框
	function ShowDialog(url, width, height, optValidate) {
		eWebEditor.focus();
		var arr = showModalDialog(url, window, "dialogWidth:" + width
				+ "px;dialogHeight:" + height
				+ "px;help:no;scroll:no;status:no");
		eWebEditor.focus();
	}

	$('#faceDv').on('click', function(){
		showFacePanal();
	});
	
	var FacePanal = null;
	var faceOne = null, faceTwo = null, faceThree = null, faceFour = null;
	function showFacePanal() {
		if (FacePanal == null) {
			var html = '';
			FacePanal = Y('#faceDiv');
			FacePanal.style.left = '275px';
			FacePanal.style.bottom = '130px';
			FacePanal.style.display = 'block';
			
			$('.facetab').on('click', function(){
				showFaces($(this).index() + 1);
			});
		} else {
			FacePanal.style.display = FacePanal.style.display == 'none' ? 'block' : 'none';
		}
		initFace(1);
	}

	function showFaces(n) {
		$('.facetab').removeClass('open');
		$('#face' + n).addClass('open');
		$('.face-panel').removeClass('open');
		$('#face' + n + 'Panal').addClass('open');
		initFace(n);
	}

	$('#editTool button').hover(function() {
		$(this).css('border', '1px double #aaaaaa');
	}, function() {
		$(this).css('border', '1px double #ffffff');
	});

	function initFace(n) {
		if (n == 1) {
			var imgName = '';
			if (faceOne == null) {
				faceOne = '<table class="face-table" cellpadding="0" cellspacing="1">';
				faceOne += '<tr>';
				for ( var i = 0; i < 100; i++) {
					if (i > 0 && i % 20 == 0) {
						faceOne += '</tr><tr>';
					}
					if (i < 96) {
						imgName = top.data.ctx + '/pages/chats/images/face1/' + i + '.gif';
						faceOne += '<td><img class="face-img" src="' + imgName + '" value="' + imgName + '"></td>';
					} else {
						faceOne += '<td>&nbsp;</td>';
					}
				}
				faceOne += '</tr></table>';
				$('#face1Panal').html(faceOne);
				
				$('#face1Panal .face-img').on('click', function(){
					addFace($(this));
				});
			}
		} else if (n == 2) {
			if (faceTwo == null) {
				faceTwo = '<table class="face-table" cellpadding="0" cellspacing="1">';
				faceTwo += '<tr>';
				for ( var i = 0; i < 60; i++) {
					if (i > 0 && i % 20 == 0) {
						faceTwo += '</tr><tr>';
					}
					if (i < 60) {
						imgName = top.data.ctx + '/pages/chats/images/face2/qq' + i + '.gif';
						faceTwo += '<td><img class="face-img" src="' + imgName + '" value="' + imgName + '"></td>';
					} else {
						faceTwo += '<td>&nbsp;</td>';
					}
				}
				faceTwo += '</tr></table>';
				$('#face2Panal').html(faceTwo);
				
				$('#face2Panal .face-img').on('click', function(){
					addFace($(this));
				});
			}
		} else if (n == 3) {
			if (faceThree == null) {
				faceThree = '<table class="face-table" cellpadding="0" cellspacing="1">';
				faceThree += '<tr>';
				for ( var i = 0; i < 60; i++) {
					if (i > 0 && i % 20 == 0) {
						faceThree += '</tr><tr>';
					}
					if (i < 60) {
						imgName = top.data.ctx + '/pages/chats/images/face3/' + i + '.gif';
						faceThree += '<td><img class="face-img" src="' + imgName + '" value="' + imgName + '"></td>';
					} else {
						faceThree += '<td>&nbsp;</td>';
					}
				}
				faceThree += '</tr></table>';
				$('#face3Panal').html(faceThree);
				
				$('#face3Panal .face-img').on('click', function(){
					addFace($(this));
				});
			}
		} else if (n == 4) {
			if (faceFour == null) {
				faceFour = '<table class="face-table" cellpadding="0" cellspacing="1">';
				faceFour += '<tr>';
				for ( var i = 0; i < 40; i++) {
					if (i > 0 && i % 20 == 0) {
						faceFour += '</tr><tr>';
					}
					if (i < 24) {
						imgName = top.data.ctx + '/pages/chats/images/face4/' + i + '.gif';
						faceFour += '<td><img class="face-img" src="' + imgName + '" value="' + imgName + '"></td>';
					} else {
						faceFour += '<td>&nbsp;</td>';
					}
				}
				faceFour += '</tr></table>';
				$('#face4Panal').html(faceFour);
				
				$('#face4Panal .face-img').on('click', function(){
					addFace($(this));
				});
			}
		}
	}

	function exeActions(data) {
		action.showAction(data);
	}

	$('#actionDv').on('click', function(){
		showActionPanal();
	});
	
	var ActionPanal = null;
	function showActionPanal() {
		if(action.timeout > 1){
			alert('动作只能每分钟发送一次，太频繁会引起公愤的 :)');
			return;
		}
		if (ActionPanal == null) {
			var html = '';
			for ( var i = 0; i < action.ActionArray.length; i++) {
				var ac = action.ActionArray[i];
				html += '<div class="action actionDv1" action="' + action.Actions[ac].action + '">';
				html += action.Actions[ac].name;
				html += '</div>';
			}
			html += '<div class="action-box"><input type="checkbox" id="localPlay"/>发送时本地播放</div>';
			ActionPanal = document.createElement("div");
			ActionPanal.style.position = 'absolute';
			ActionPanal.style.width = '440px';
			ActionPanal.style.height = '35px';
			ActionPanal.style.border = '1px double #b98841';
			ActionPanal.style.backgroundColor = '#eeeeee';
			ActionPanal.style.left = '275px';
			ActionPanal.style.bottom = '130px';
			ActionPanal.style.fontSize = '12px';
			ActionPanal.innerHTML = html;
			document.body.appendChild(ActionPanal);
			
			$('.action').hover(function(){
				$(this).addClass('open');
			}, function(){
				$(this).removeClass('open');
			}).on('click', function(){
				var ischeck = $('#localPlay')[0].checked;
				action.sendActions($(this).attr('action'), $('#touser-id').text(), showActionPanal, ischeck);
			});
		} else {
			ActionPanal.style.display = ActionPanal.style.display == 'none' ? 'block' : 'none';
		}
	}

	var setPanal = null;
	var backArray = [ 'chat.png', 'chat2.png', 'chat3.png', 'chat4.png' ];
	function showSystemSet() {
		if (setPanal == null) {
			var html = '皮肤：<select id="pifu" onchange="changePifu(this)">';
			for ( var i = 0; i < backArray.length; i++) {
				var ac = backArray[i];
				html += '<option value="' + ac + '">';
				html += '皮肤' + (i + 1);
				html += '</option>';
			}
			html += '</select><br/><button onclick="showSystemSet()">关闭</button>';
			setPanal = document.createElement("div");
			var sty = J(document).getPosition(Y('#setImg'));
			setPanal.style.position = 'absolute';
			setPanal.style.width = '200px';
			setPanal.style.height = '60px';
			setPanal.style.border = '1px double #b98841';
			setPanal.style.backgroundColor = '#eeeeee';
			setPanal.style.left = sty.left - 190;
			setPanal.style.top = sty.top + 20;
			setPanal.style.fontSize = '12px';
			setPanal.innerHTML = html;
			document.body.appendChild(setPanal);
		} else {
			setPanal.style.display = setPanal.style.display == 'none' ? 'block'
					: 'none';
		}
	}

	function changePifu(t) {
		var url = 'url(' + top.data.ctx + '/pages/chats/images/' + t.options.value + ')';
		Y('#Main').style.backgroundImage = url;
	}

	function showImage(t) {
		var url = t.options.value;
		Y('#avatar').style.display = 'block';
		Y('#avatarImg').src = url;
	}

	function hidImage() {
		Y('#avatar').style.display = 'none';
	}

	function changeUserListCss(t, n) {
		if (n == 1) {
			$(t).addClass('hover');
		} else {
			$(t).removeClass('hover');
		}
	}

	function chatmode(n) {
		if (n == 1) {
			$('#chatallBtn').addClass('act');
			$('#chatBtn').removeClass('act');
			$('#chatBtn').attr('disabled', true);
			$('#touser').val('大家');
			$('#touser').attr('userId', 'all_user');
		} else {
			$('#chatallBtn').removeClass('act');
			$('#chatBtn').addClass('act');
			$('.userDv').each(function() {
				if ($(this).hasClass('active')) {
					$('#touser').val($(this).attr('title'));
					$('#touser').attr('userId', $(this).attr('id'));
				}
			});
		}
	}
	
	
	//-------------------------------------
	var header = 45, fgx = 6, pad = 3, padding = 10, bor = 1;
	var SetPanelStyle = function(){
		var screenWidth = $(window).width();
        var screenHeight = $(window).height();
        var leftmain = screenWidth - 210;
        var leftPanel = 260, bomhei = 130, btnwid = 100, toolhei = 30;
        $('#left-main').css({'width': leftmain + 'px', 'height': screenHeight + 'px'});
        $('#right-main').css({'width': (screenWidth - leftmain) + 'px', 'height': (screenHeight < 500 ? 500 : screenHeight) + 'px'});
        $('#userListDiv').css({'width': (leftPanel) + 'px', 'height': (screenHeight - bomhei - fgx) + 'px'});
        $('#info').css({'width': (leftmain - leftPanel - fgx - padding) + 'px', 'height': (screenHeight - bomhei - fgx - padding) + 'px'});
        $('#myInfo').css({
        	'width': leftPanel + 'px', 
        	'height': (bomhei) + 'px',
        	'margin-top': pad + 'px'
        });
        $('#editcontent').css({
        	'width': (leftmain - leftPanel - fgx) + 'px', 
        	'height': (bomhei)+ 'px',
        	'margin-top': pad + 'px'
        });
        $('#content').css({
        	'width': (leftmain - leftPanel - fgx - btnwid) + 'px', 
        	'height': (bomhei - fgx - toolhei) + 'px',
        	'margin-top': pad + 'px'
        });
        $('#btn-panel').css({
        	'width': (btnwid - pad) + 'px', 
        	'height': (bomhei - fgx - toolhei) + 'px',
        	'margin-top': pad + 'px'
        });
	};
	SetPanelStyle();
	$(window).resize(function() {
		SetPanelStyle();

	});
	

});